<?php

namespace app\admin\controller;

use app\admin\model\Adminuser;
use think\Request;


class Index extends Middleware
{

    //首页
    public function index()
    {
        /** 前置登录检测 */
        $this -> islogin();
        return $this -> fetch();
    }

    //登录
    public function login()
    {
        $this -> view -> engine -> layout(false);//关闭模板布局
        return $this -> fetch('login');
    }

    //登录的检测操作
    public function loging(Request $request)
    {
        //接受数据
        $username = $request -> post('username');
        $password = $request -> post('password');
        $model = new Adminuser();
        $res = $model -> checklogin($username, $password);
        if ($res) {
            session('useradmin', $res -> admin);
            return $this -> redirect(url('admin/index/index'));
        }
        return $this -> error('用户名或者密码错误', url('admin/index/login'));
    }

    //登出操作
    public function logout()
    {
        session('useradmin', null);
        return $this -> redirect(url('admin/index/login'));
    }
}